﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System.Data;
 

public partial class HARISH_SIR_PDFHtm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_show_Click(object sender, EventArgs e)
    {
        string name = lbl_id.Text;
        String code = " <table border=1><tr><td><asp:Label ID='lbl_id' runat='server' Text='Row1Column1'></asp:Label></td><td><asp:Label ID='Label1' runat='server' Text='Row1Column2'></asp:Label>"
                     + " </td><td colspan='2'><center>"+name+"ERTERTERT<asp:Label ID='Label2' runat='server' Text='RowColumn3'></asp:Label></center>"

                     + "  </td></tr><tr><td><asp:Label ID='Label4' runat='server' Text=''></asp:Label></td><td><asp:Label ID='Label5' runat='server' Text=''></asp:Label>"
                      +"  </td><td><asp:Label ID='Label6' runat='server' Text=''></asp:Label></td><td><asp:Label ID='Label7' runat='server' Text=''></asp:Label>"
                       +" </td></tr><tr><td><asp:Label ID='Label8' runat='server' Text=''></asp:Label>"
                        +"</td><td><asp:Label ID='Label9' runat='server' Text=''></asp:Label></td><td><asp:Label ID='Label10' runat='server' Text=''></asp:Label>"
                       +" </td><td><asp:Label ID='Label11' runat='server' Text=''></asp:Label></td></tr><tr>"
                       +" <td colspan='4' style='border: 1px solid black;'><center><asp:Label ID='Label12' runat='server' Text='Middle Of the Table'></asp:Label>"
                         +"   </center></td></tr><tr><td colspan='2'><asp:Label ID='Label16' runat='server' Text='Column1'></asp:Label>"
                        +"</td><td colspan='2'><asp:Label ID='Label17' runat='server' Text='Column2'></asp:Label>"
                        +"</td></tr><tr><td colspan='2'><asp:Label ID='Label20' runat='server' Text=' '></asp:Label>"
                        +"</td><td colspan='2'><asp:Label ID='Label21' runat='server' Text='Column2'></asp:Label>"
                        +"</td></tr><tr><td colspan='2'><asp:Label ID='Label24' runat='server' Text=''></asp:Label>"
                        +"</td><td colspan='2'><asp:Label ID='Label25' runat='server' Text='Column2'></asp:Label>"
                        +"</td></tr><tr><td style='border: 1px solid black;'><asp:Label ID='Label28' runat='server' Text=''></asp:Label>"
                       +" </td><td style='border: 1px solid black;'><asp:Label ID='Label29' runat='server' Text=''></asp:Label>"
                       +" </td><td style='border: 1px solid black;'><asp:Label ID='Label30' runat='server' Text=''></asp:Label>"
                       +" </td><td style='border: 1px solid black;'><asp:Label ID='Label31' runat='server' Text=''></asp:Label>"
                       +" </td></tr><tr><td><asp:Label ID='Label32' runat='server' Text=''></asp:Label></td><td><asp:Label ID='Label33' runat='server' Text=''></asp:Label></td>"
                       +" <td><asp:Label ID='Label34' runat='server' Text=''></asp:Label></td>"
                       +" <td><asp:Label ID='Label35' runat='server' Text=''></asp:Label></td></tr><tr style='border-bottom: 1px solid black; border-top: 1px solid black;'><td>"
                         +"   <asp:Label ID='Label36' runat='server' Text=''></asp:Label></td><td>"
                           +" <asp:Label ID='Label37' runat='server' Text=''></asp:Label></td><td>"
                           +" <asp:Label ID='Label38' runat='server' Text=''></asp:Label></td><td>"
                           +" <asp:Label ID='Label39' runat='server' Text=''></asp:Label></td></tr></table>";

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        maindiv.RenderControl(hw);
       
        StringReader sr = new StringReader(code);
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();    
        //HTMLWorker worker = new HTMLWorker(pdfDoc);
        //String code = "<table border=1><tr><td>Test</td><td>Test</td></tr></table>";
        //StringReader sr2 = new StringReader(code);
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }
}